package vn.edu.uit.chemistry;

import java.util.Map;

import vn.edu.uit.chemistry.view.ViewRowGameShow;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Chronometer;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class CapNhatGameShow extends Activity {

	ImageView imgTheoDoi;
	Chronometer tvGio;

	public static LinearLayout lvGame;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_kq_giai_tri);

		imgTheoDoi = (ImageView) findViewById(R.id.imgView);
		lvGame = (LinearLayout) findViewById(R.id.lvGame);
		tvGio = (Chronometer) findViewById(R.id.tvGio);

		imgTheoDoi.postDelayed(onEverySecond, 0);
		tvGio.start();
	}

	public Runnable onEverySecond = new Runnable() {
		public void run() {
			new UpdatePrecentAsyncTask(getApplicationContext()).execute();
		}
	};

	final class UpdatePrecentAsyncTask extends AsyncTask<Void, Void, Void> {

		private Context context;
		private ViewRowGameShow viewGame;
		private Map<String, ?> keys;

		public UpdatePrecentAsyncTask(Context ctx) {
			this.context = ctx;
		}

		@Override
		protected Void doInBackground(Void... param) {
			SharedPreferences prefs = getSharedPreferences("DSNhom", 0);
			keys = prefs.getAll();
			return null;
		}

		@Override
		protected void onPostExecute(final Void unused) {
			lvGame.removeAllViews();
			for (Map.Entry<String, ?> entry : keys.entrySet()) {
				String[] values = entry.getValue().toString().split("#");
				viewGame = new ViewRowGameShow(context);
				viewGame.setContent(values[0], values[1]);
				lvGame.addView(viewGame.layout);
			}
			imgTheoDoi.postDelayed(onEverySecond, 0);
		}

	}
}
